﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.LearningTrain.DriverAbsenteeismRecord;

namespace BCData.TMS.LearningTrain
{
    public class DriverAbsenteeismRecordData : IDriverAbsenteeismRecordData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    DriverAbsenteeismRecordId,
		    DriverAbsenteeismRecordCode,
		    DriverId,
		    PendingLearningVehicleId,
		    VehicleTrainingScheduleRecordId,
		    Mark,
		    CreateTime,
		    CreateAdminId,
		    CreateAdminName
        FROM
            DriverAbsenteeismRecord";



        #region 构造函数
        public DriverAbsenteeismRecordData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新司机缺席记录表纪录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<DriverAbsenteeismRecordEntity> InsertAsync(DriverAbsenteeismRecordEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改司机缺席记录表记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<DriverAbsenteeismRecordEntity> UpdateAsync(DriverAbsenteeismRecordEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除司机缺席记录表记录
        /// </summary>
        /// <param name="driverAbsenteeismRecordId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long driverAbsenteeismRecordId)
        {
            string sql = "DELETE FROM DriverAbsenteeismRecord WHERE DriverAbsenteeismRecordId = ?driverAbsenteeismRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DriverAbsenteeismRecordId", driverAbsenteeismRecordId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取司机缺席记录表记录
        /// </summary>
        /// <param name="driverAbsenteeismRecordId"></param>
        /// <returns></returns>
        public async Task<DriverAbsenteeismRecordEntity> GetEntityAsync(long driverAbsenteeismRecordId)
        {
            string sql = QUERY + " WHERE  DriverAbsenteeismRecordId = ?driverAbsenteeismRecordId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DriverAbsenteeismRecordId", driverAbsenteeismRecordId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<DriverAbsenteeismRecordEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询司机缺席记录表记录
        public async Task<(IEnumerable<DriverAbsenteeismRecordEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  DriverAbsenteeismRecord  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<DriverAbsenteeismRecordEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口







